/* Revision 2, Karl*/
// Because we all know arrays start at 0
#include <iostream>
#include <sstream>
#include <string>

/* Revision 3, Minnek */
// Added demo SDL; altered SDL 
#include "bengine/FBGraphics.h"
#include "game/CSDLGraphics.h"
#include "game/CSDLTimekeeper.h"
#include "platfinc.h"


using std::cout; using std::endl;
using namespace FBShells;

int main(int argc, char* argv[]) {
	char s[100];

	//set our at exit function
	atexit(SDL_Quit);
	if(!SDL_VideoDriverName(s, 100))
		cout<<"No Display Driver found."<<endl;
	else
		cout<<s<<endl;
	
	FBGraphics.Initialize(new CSDLGraphics);
	FBGraphics.Instance()->Initialize(800,600,false);
	FBTimekeeper.Initialize(new CSDLTimekeeper);
	FBTimekeeper.Instance()->initialize();
	FBTimekeeper.Instance()->setFPS(60);
	
	
	//create a window
	SDL_Surface* pSurface = ((CSDLGraphics*)FBGraphics.Instance())->GetScreen();

	SDL_Surface* thing = IMG_Load("thing.png");	
	if(!thing)
		cout<<"WTF"<<endl;
		
//	SDL_FillRect(pSurface,NULL,SDL_MapRGB(pSurface->format,32,32,32));	
//	SDL_UpdateRect(pSurface,0,0,0,0);
	
	if(SDL_SetColorKey(thing,SDL_SRCCOLORKEY,SDL_MapRGB(thing->format,255,0,255)) == -1)
		cout<<"Problem?! o_o"<<endl;
    //SDL_SetAlpha(thing,SDL_SRCALPHA|SDL_RLEACCEL,128);
	
	cout<<"Doing SDL Rectangles now."<<endl;

	SDL_Rect rcSrc, rcDst;
	rcSrc.w = thing->w;
	rcSrc.h = thing->h;
	rcSrc.x = 0;
	rcSrc.y = 0;
	rcDst = rcSrc;
	rcDst.x = 600;
	rcDst.y = 250;

	// Message pump.
	SDL_Event event;
	bool quitgame = false;
	bool down,up,left,right;
	down = up = left = right = false;
	while(!quitgame)
	{
		if(down)
			rcDst.y += 20;
		if(up)
			rcDst.y -= 20;
		if(left)
			rcDst.x -= 20;
		if(right)
			rcDst.x += 20;
		
		if(SDL_PollEvent(&event))
		{
			if(event.type == SDL_QUIT) 
				break;
			if(event.type == SDL_KEYDOWN) {
				SDLKey& tehkey = event.key.keysym.sym;
				switch(tehkey) {
				
					case SDLK_UP:
						up = true;
						break;
						
					case SDLK_DOWN:
						down = true;
						break;
						
					case SDLK_LEFT:
						left = true;
						break;
						
					case SDLK_RIGHT:
						right = true;
						break;
				
					case SDLK_ESCAPE:
						quitgame = true;
					break;
						
					default:
						break;
				}
			}
			if(event.type == SDL_KEYUP) {
				SDLKey& tehkey = event.key.keysym.sym;
				switch(tehkey) {
				
					case SDLK_UP:
						up = false;
						break;
						
					case SDLK_DOWN:
						down = false;
						break;
						
					case SDLK_LEFT:
						left = false;
						break;
						
					case SDLK_RIGHT:
						right = false;
						break;
				
					case SDLK_ESCAPE:
						quitgame = false;
					break;
						
					default:
						break;
				}
			}
		}	
	    //clear the screen
	    SDL_FillRect(pSurface,NULL,0);

	    std::string temp; temp += "FPS = ";
	    std::ostringstream ff; ff << FBTimekeeper.Instance()->getFPS();

	    temp += ff.str();
	    
	    SDL_WM_SetCaption(temp.c_str(), NULL);
	    std::string lol = "LOL";
	    SDL_BlitSurface(thing,&rcSrc,pSurface,&rcDst);
	    FBGraphics.Instance()->drawString(123,705,lol,CSDLColor(0,0,0,0),CSDLColor(0,0,0,0));
	    FBGraphics.Instance()->drawString(524,551,lol,CSDLColor(0,0,0,0),CSDLColor(0,0,0,0));
	    FBGraphics.Instance()->drawString(333,431,lol,CSDLColor(0,0,0,0),CSDLColor(0,0,0,0));
	    FBGraphics.Instance()->drawString(398,311,lol,CSDLColor(0,0,0,0),CSDLColor(0,0,0,0));
	    FBGraphics.Instance()->drawString(444,213,lol,CSDLColor(0,0,0,0),CSDLColor(0,0,0,0));
	    FBGraphics.Instance()->drawString(0,523,lol,CSDLColor(0,0,0,0),CSDLColor(0,0,0,0));
	    
	    
	    FBGraphics.Instance()->renderScreen();
	    FBTimekeeper.Instance()->renderDelay();
	}
	SDL_FreeSurface(thing);
	//SDL_FreeSurface(lawl);
	return (0);
}
